useImg = undefined;
/**
 *  API
 * ---------------------
 *  getDefIcon()
 *
 */
const SVGUtil = (function () {

    'use strict';

    const ns = 'http://www.w3.org/2000/svg';
    const xmlns = 'http://www.w3.org/2000/xmlns/';
    const xlink = 'http://www.w3.org/1999/xlink';

    // 图标缓存
    const ICON_MAP = {};

    // 分支图标设置，  branch 开头
    ICON_MAP["branch_auto_force"] = {
        width: 24,
        height: 24,
        imageData: ""
    };

    // 节点头像图标设置， nodeicon 开头
    ICON_MAP["nodeicon_FormField"] = {
        width: 32,
        height: 32,
        imageData: ""
    };

    const DEFAULT_ICON = {
        width : 32,
        height: 32,
        imageData:"icon/user-14.gif"
    };



    const SVGUtil = function () {

        this.supported = (function () {
            return !!document.createElementNS &&
                !!document.createElementNS(ns, 'svg').createSVGRect
        })();
    };

    SVGUtil.prototype = {
        create: function (name) {

            let element;
            //if(document.createElementNS){
            element = document.createElementNS(ns, name);
            /*}else{
                element = document.createElement(name)
            }*/
            if (name.toLowerCase() === "svg") {
                element.setAttribute("xmlns", ns);
                element.setAttribute("version", "1.1");
                element.setAttribute("xmlns:xlink", xlink);
                element.setAttribute("xmlns:workflow", "http://www.seeyon.com/");
            }
            return element
        },
        newText: function (text) {
            return document.createTextNode(text);
        },
        setAttr: function (el, att, value) {
            if (att.indexOf("xlink:") === 0) {
                el.setAttributeNS(xlink, att, value);
            } else {
                el.setAttribute(att, value);
            }
        },
        getAttr: function (el, att) {
            if (att.indexOf("xlink:") === 0) {
                return el.getAttributeNS(xlink, att);
            } else {
                return el.getAttribute(att);
            }
        }
    };


    SVGUtil.prototype.getDefIcon = function (iconName, userDef) {

        let data = ICON_MAP[iconName];
        const image = this.create("image");
        let isImage = false;
        let retNode;

        if(!data){
            if(!userDef){
                // 使用默认的数据
                data = DEFAULT_ICON;
            }else{
                data = userDef;
                isImage = userDef.useImg === true;
            }
        }else if(userDef){
            // 合并下width 和 height
            if(userDef.width){
                data.width = userDef.width;
            }

            if(userDef.height){
                data.height = userDef.height;
            }

            isImage = userDef.useImg === true;
        }

        this.setAttr(image, "width", data.width);
        this.setAttr(image, "height", data.height);
        //this.setAttr(image, "xlink:href", data.imageData);
        this.setAttr(image, "href", data.imageData);

        retNode = image;

        if(!isImage){

            //
            //this.setAttr(image, "preserveAspectRatio", "none");

            const pattern = this.create("pattern");
            //this.setAttr(pattern, "patternUnits", "userSpaceOnUse");
            //this.setAttr(pattern, "width", data.width);
            //this.setAttr(pattern, "height", data.height);
            this.setAttr(pattern, "patternUnits", "objectBoundingBox");
            this.setAttr(pattern, "width", "1");
            this.setAttr(pattern, "height", "1");
            this.setAttr(pattern, "x", "0");
            this.setAttr(pattern, "y", "0");

            pattern.appendChild(image);

            retNode = pattern;
        }

        return retNode;
    };

    return new SVGUtil();
})();
